package 剑指offer.demo6;

import java.util.ArrayList;

//https://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe?
/*
跟《二叉树中和为某一值的路径_2》这道题有几分相似
 */
public class 和为S的连续正数序列 {
    public ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();

        int start = 1;
        int end = 2;
        while (start < end){
            int cur = (start + end ) * (end - start + 1) / 2;
            if (cur == sum){
                ArrayList<Integer> list = new ArrayList<>();
                for (int i = start; i <= end; i++) {
                    list.add(i);
                }
                result.add(list);
                //这个就是点睛之笔，在后面有想等情况一定不会有前面值，把他更新避免死循环
                start++;
            }
            if (cur < sum)
                end ++;
            if (cur > sum)
                start ++;
        }
        return result;
    }
}
